﻿using System;
using System.Collections.Generic;
using System.Text;
using LabMS.BLL;
using LabMS.Model;
using Maticsoft.DBUtility;
using System.Data;//请先添加引用

namespace ExtendBLL
{
    public class InstrumentAndConsumables : LabMS.BLL.InstrumentAndConsumables
    {

       

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "InstrumentAndConsumables");
        }

        public bool isNumbersLegal(string strNumbers, string strType)
        {
            LabMS.BLL.InstrumentAndConsumables bconsum = new LabMS.BLL.InstrumentAndConsumables();
            List<LabMS.Model.InstrumentAndConsumables> listmconsum
                = new List<LabMS.Model.InstrumentAndConsumables>();
            listmconsum = bconsum.GetModelList("Numbers='" + strNumbers + "' and Type='" + strType + "'");
            if (listmconsum.Count > 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<ExtendModule.InstrumentAndConsumables> GetExtendModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            List<ExtendModule.InstrumentAndConsumables> modelList = new
                List<ExtendModule.InstrumentAndConsumables>();
            int rowsCount = ds.Tables[0].Rows.Count;
            if (rowsCount > 0)
            {
                ExtendModule.InstrumentAndConsumables model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new ExtendModule.InstrumentAndConsumables();
                    model.ID= long.Parse(ds.Tables[0].Rows[n]["ID"].ToString());
                    model.Numbers = ds.Tables[0].Rows[n]["Numbers"].ToString();
                    model.Type = ds.Tables[0].Rows[n]["Type"].ToString();
                    model.ClassNumber = ds.Tables[0].Rows[n]["ClassNumber"].ToString();
                    model.Name = ds.Tables[0].Rows[n]["Name"].ToString();
                    model.Standards = ds.Tables[0].Rows[n]["Standards"].ToString();
                    model.Model = ds.Tables[0].Rows[n]["Model"].ToString();
                    model.CountryCode = ds.Tables[0].Rows[n]["CountryCode"].ToString();
                    model.StockLocation = ds.Tables[0].Rows[n]["StockLocation"].ToString();
                    model.SpaceLocation = ds.Tables[0].Rows[n]["SpaceLocation"].ToString();
                    if (ds.Tables[0].Rows[n]["Quantity"].ToString() != "")
                    {
                        model.Quantity = decimal.Parse(ds.Tables[0].Rows[n]["Quantity"].ToString());
                    }
                    model.FactoryCode = ds.Tables[0].Rows[n]["FactoryCode"].ToString();
                    model.UnitPrice = ds.Tables[0].Rows[n]["UnitPrice"].ToString();
                    model.Documents = ds.Tables[0].Rows[n]["Documents"].ToString();
                    model.Measurement = ds.Tables[0].Rows[n]["Measurement"].ToString();
                    model.Source = ds.Tables[0].Rows[n]["Source"].ToString();
                    if (ds.Tables[0].Rows[n]["PurchaseDate"].ToString() != "")
                    {
                        model.PurchaseDate = DateTime.Parse(ds.Tables[0].Rows[n]["PurchaseDate"].ToString());
                    }
                    if (ds.Tables[0].Rows[n]["ProductionDate"].ToString() != "")
                    {
                        model.ProductionDate = DateTime.Parse(ds.Tables[0].Rows[n]["ProductionDate"].ToString());
                    }
                    model.Arrow = ds.Tables[0].Rows[n]["Arrow"].ToString();
                    if (ds.Tables[0].Rows[n]["CreatedDate"].ToString() != "")
                    {
                        model.CreatedDate = DateTime.Parse(ds.Tables[0].Rows[n]["CreatedDate"].ToString());
                    }
                    model.FundsSubject = ds.Tables[0].Rows[n]["FundsSubject"].ToString();
                    model.Status = ds.Tables[0].Rows[n]["Status"].ToString();
                    model.ResponesoblePerson = ds.Tables[0].Rows[n]["ResponesoblePerson"].ToString();
                    model.ContactPhone = ds.Tables[0].Rows[n]["ContactPhone"].ToString();
                    model.Purpose = ds.Tables[0].Rows[n]["Purpose"].ToString();
                    if (ds.Tables[0].Rows[n]["OpeningTime"].ToString() != "")
                    {
                        model.OpeningTime = DateTime.Parse(ds.Tables[0].Rows[n]["OpeningTime"].ToString());
                    }
                    model.Charges = ds.Tables[0].Rows[n]["Charges"].ToString();
                    model.ZipCode = ds.Tables[0].Rows[n]["ZipCode"].ToString();
                    model.MailingAddress = ds.Tables[0].Rows[n]["MailingAddress"].ToString();
                    model.PresentSituation = ds.Tables[0].Rows[n]["PresentSituation"].ToString();
                    model.Operator = ds.Tables[0].Rows[n]["Operator"].ToString();
                    if (ds.Tables[0].Rows[n]["OperatorDate"].ToString() != "")
                    {
                        model.OperatorDate = DateTime.Parse(ds.Tables[0].Rows[n]["OperatorDate"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }


       /// <summary>
        /// 获得统计数据列表
       /// </summary>
       /// <param name="strWhere"></param>
       /// <returns></returns>
        public List<ExtendModule.InstrumentAndConsumables> GetExtendStatisticsModelList(string strWhere)
        {
            string strQuery = "select c.[ID],c.Numbers,c.[Name],c.Quantity,c.UnitPrice, c.StockLocation,sum(cast(r.QuantityRecipients as int)) as totalusenub from InstrumentAndConsumables c left join SuppliesRecipients r on c.Numbers = r.Numbers "; 
            if (!strWhere.Equals(""))
            {
                strQuery += "where " + strWhere;
            }
            strQuery += " group by c.[id], c.Numbers ,c.[Name],c.Quantity,c.UnitPrice,c.StockLocation  order by c.[ID] desc ";
            DataSet ds = DbHelperSQL.Query(strQuery);
            List<ExtendModule.InstrumentAndConsumables> modelList = new
                List<ExtendModule.InstrumentAndConsumables>();
           
            
            int rowsCount = ds.Tables[0].Rows.Count;
            if (rowsCount > 0)
            {
                ExtendModule.InstrumentAndConsumables model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new ExtendModule.InstrumentAndConsumables();
                    model.ID = long.Parse(ds.Tables[0].Rows[n]["ID"].ToString());
                    model.Numbers = ds.Tables[0].Rows[n]["Numbers"].ToString();
                    model.Name = ds.Tables[0].Rows[n]["Name"].ToString();
                    model.StockLocation = ds.Tables[0].Rows[n]["StockLocation"].ToString();
                    if (ds.Tables[0].Rows[n]["Quantity"].ToString() != "")
                    {
                        model.Quantity = decimal.Parse(ds.Tables[0].Rows[n]["Quantity"].ToString());
                    }
                    model.UnitPrice = ds.Tables[0].Rows[n]["UnitPrice"].ToString();
                   
                    if (ds.Tables[0].Rows[n]["totalusenub"] == null)
                    {
                        model.TotalUseNub = "";
                        model.TotalNub = model.Quantity.ToString();
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(ds.Tables[0].Rows[n]["totalusenub"].ToString()))
                        {
                            model.TotalUseNub = "0";
                            model.TotalNub = model.Quantity.ToString();
                        }
                        else
                        {
                            decimal itemp = 0;
                            decimal.TryParse(ds.Tables[0].Rows[n]["totalusenub"].ToString(), out itemp);
                            model.TotalUseNub = itemp.ToString();
                            itemp = model.Quantity.Value + itemp;
                            model.TotalNub = itemp.ToString();
                        }
                    }

                    modelList.Add(model);
                }
            }
            return modelList;
        }
    }
}
